package com.talkyun.utils;

import java.util.Map;

public class MapUtils {

    public static String getString(Map map, String... keys) {
        for (String key : keys) {
            String val = getFromMap(map, key);
            if (isNotBlank(val)) {
                return val.trim();
            }
        }
        return null;
    }

    public static String getStringFromMaps(Map[] maps, String... keys) {
        for (Map map : maps) {
            String val = getString(map, keys);
            if (isNotBlank(val)) {
                return val.trim();
            }
        }
        return null;
    }

    private static String getFromMap(Map map, String key) {
        key = key.trim();
        for (Object o : map.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            if (entry.getKey() == null || entry.getValue() == null) {
                continue;
            }
            String tmp = entry.getKey().toString().trim();
            if (key.equalsIgnoreCase(tmp)) {
                return entry.getValue().toString().trim();
            }
        }
        return null;
    }

    private static boolean isNotBlank(String s) {
        return s != null && s.trim().length() != 0;
    }
}
